from flask_restful import Resource, reqparse
from flask import request
from models import db, DishCategory
from utils import api_response

class CategoryList(Resource):
    def get(self):
        """获取菜品分类列表"""
        parser = reqparse.RequestParser()
        parser.add_argument('status', type=int, location='args')
        parser.add_argument('page', type=int, default=1, location='args')
        parser.add_argument('size', type=int, default=10, location='args')
        args = parser.parse_args()
        
        query = DishCategory.query
        if args['status'] is not None:
            query = query.filter_by(status=args['status'])
        
        categories = query.paginate(
            page=args['page'], 
            per_page=args['size'], 
            error_out=False
        )
        
        return api_response({
            'total': categories.total,
            'page': args['page'],
            'size': args['size'],
            'categories': [category.to_dict() for category in categories.items]
        })
    
    def post(self):
        """创建菜品分类"""
        parser = reqparse.RequestParser()
        parser.add_argument('name', type=str, required=True, help='分类名称不能为空')
        parser.add_argument('description', type=str)
        parser.add_argument('sort_order', type=int, default=0)
        parser.add_argument('status', type=int, default=1)
        args = parser.parse_args()
        
        category = DishCategory(
            name=args['name'],
            description=args['description'],
            sort_order=args['sort_order'],
            status=args['status']
        )
        
        db.session.add(category)
        db.session.commit()
        
        return api_response(category.to_dict(), message='分类创建成功')

class CategoryDetail(Resource):
    def get(self, category_id):
        """获取菜品分类详情"""
        category = DishCategory.query.get(category_id)
        if not category:
            return api_response({'message': '分类不存在'}, code=404)
        
        return api_response(category.to_dict())
    
    def put(self, category_id):
        """更新菜品分类"""
        category = DishCategory.query.get(category_id)
        if not category:
            return api_response({'message': '分类不存在'}, code=404)
        
        parser = reqparse.RequestParser()
        parser.add_argument('name', type=str)
        parser.add_argument('description', type=str)
        parser.add_argument('sort_order', type=int)
        parser.add_argument('status', type=int)
        args = parser.parse_args()
        
        if args['name'] is not None:
            category.name = args['name']
        if args['description'] is not None:
            category.description = args['description']
        if args['sort_order'] is not None:
            category.sort_order = args['sort_order']
        if args['status'] is not None:
            category.status = args['status']
        
        db.session.commit()
        
        return api_response(category.to_dict(), message='分类更新成功')
    
    def delete(self, category_id):
        """删除菜品分类"""
        category = DishCategory.query.get(category_id)
        if not category:
            return api_response({'message': '分类不存在'}, code=404)
        
        db.session.delete(category)
        db.session.commit()
        
        return api_response(None, message='分类删除成功')